package com.httptcp;

import java.io.*;
import java.net.Socket;

public class HttpThread extends Thread {
    private Socket socket;

    public HttpThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            // 获取输入输出流
            BufferedReader bis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            BufferedReader fBis = new BufferedReader(new FileReader("net-study\\src\\com\\httptcp\\标题排版.html"));
            // 以为浏览器每次只请求一次，这里不用做死循环
            String request = bis.readLine();
            StringBuffer sb = new StringBuffer();
            String response = "";
            while ((response = fBis.readLine()) != null) {
                sb.append(response);
            }

            if (request != null) {
                if (request.contains("GET")) {
                    bos.write("HTTP/1.1 200 OK\r\n");
                    bos.write("Content-Type: text/html\r\n");
                    bos.write("\r\n");
                    bos.write(sb.toString());
                    bos.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
